'''
@File    :   ReadDTCInfomation_NRC.py
@Time    :   2025/04/07 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangl@genvict.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from gettext import find
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True
import Set_up

#NRC13
ReadDTCInfo_NRC13_phy = [   
    ("19", "7F 19 13"),                       
    ("19 01", "7F 19 13"),
    ("19 02", "7F 19 13"), 
    ("19 04", "7F 19 13"), 
    ("19 06", "7F 19 13"), 
    ("19 01 01 02", "7F 19 13"),
    ("19 02 01 02", "7F 19 13"),
    ("19 04 F0", "7F 19 13"), 
    ("19 04 F0 03", "7F 19 13"), 
    ("19 04 91 11 16", "7F 19 13"), 
    ("19 04 91 11 16 01 02", "7F 19 13"), 
    ("19 06 F0", "7F 19 13"), 
    ("19 06 F0 03", "7F 19 13"), 
    ("19 06 91 11 16", "7F 19 13"), 
    ("19 06 91 11 16 01 02", "7F 19 13"), 
    ("19 0A 01", "7F 19 13"),
    ("19 0A 01 02", "7F 19 13"),
    ("19 0A 01 02 03", "7F 19 13"),
    ("19 0A 01 02 03 04", "7F 19 13"),
] 

#NRC12
ReadDTCInfo_NRC12_phy = [   
    ("19 08 01 02", "7F 19 12"),            
    ("19 00 01 02 03 04", "7F 19 12"), 
    ("19 05 01", "7F 19 12"), 
    ("19 07 01 02", "7F 19 12"),
    ("19 09 01 02 03", "7F 19 12"),
    ("19 11 01 02 03", "7F 19 12"),
    ("19 12 01 02 03", "7F 19 12"),
    ("19 13", "7F 19 12"),
    ("19 14", "7F 19 12"),
    ("19 15", "7F 19 12"),
    ("19 16", "7F 19 12"),
    ("19 00", "7F 19 12"),
]

#NRC31
# 处理 Set_up.DTC_list 部分
dtc_list_part_04 = [("19 04 " + bytes(item[0]).hex(' ').upper() + " 02", "7F 19 31") for item in Set_up.DTC_list]
dtc_list_part_06 = [("19 06 " + bytes(item[0]).hex(' ').upper() + " 03", "7F 19 31") for item in Set_up.DTC_list]
fixed_part  = [ 
    ("19 04 D0 A2 87 00", "7F 19 31"),
    ("19 04 D0 97 87 03", "7F 19 31"),
    ("19 04 C0 7B 88 04", "7F 19 31"),
    ("19 04 91 11 16 09", "7F 19 31"),
    ("19 04 F0 03 17 A0", "7F 19 31"),
    ("19 04 D3 05 55 A1", "7F 19 31"),
    ("19 04 D3 05 56 A8", "7F 19 31"),
    ("19 06 D0 A2 87 00", "7F 19 31"),
    ("19 06 D0 97 87 05", "7F 19 31"),
    ("19 06 C0 7B 88 06", "7F 19 31"),
    ("19 06 91 11 16 09", "7F 19 31"),
    ("19 06 F0 03 17 A0", "7F 19 31"),
    ("19 06 D3 05 55 A1", "7F 19 31"),
    ("19 06 D3 05 56 A8", "7F 19 31"),
]
ReadDTCInfo_NRC31_phy = dtc_list_part_04 + dtc_list_part_06 + fixed_part

#NRC13
ReadDTCInfo_NRC12_Priority = [  
    ("19 08", "7F 19 12"),            
    ("19 07 01 02", "7F 19 12"), 
    ("19 05", "7F 19 12"), 
    ("19 07", "7F 19 12"),
    ("19 11", "7F 19 12"),
    ("19 12", "7F 19 12"),
    ("19 13", "7F 19 12")
]

ReadDTCInfo_NRC_no_resp_Fun = [ 
    ("19 01", ""), 
    ("19 02", ""), 
    ("19 06", ""),
    ("19 05", ""),          
    ("19 0A", ""), 
    ("19 05", ""), 
    ("19 07", ""), 
    ("19 04 5C 1A 90 01", ""),
    ("19 04 11 22 33 01", ""), 
    ("19 04 5C 03 17 00", "")
]

#NRC12
ReadDTCInfo_NRC11_phy = [   
    ("19 01 01", "7F 19 11"),
    ("19 01 08", "7F 19 11"),
    ("19 01 09", "7F 19 11"),
    ("19 01 FF", "7F 19 11"),
    ("19 02 01", "7F 19 11"),
    ("19 02 08", "7F 19 11"),
    ("19 02 09", "7F 19 11"),
    ("19 02 0A", "7F 19 11"),
    ("19 02 0B", "7F 19 11"),
    ("19 02 FF", "7F 19 11"),
    ("19 04 AC D1 78 01", "7F 19 11"),
    ("19 06 AC D2 78 01", "7F 19 11"),
    ("19 06 AC D2 78 02", "7F 19 11"),
]

class Test_APP_ReadDTCInfomation_NRC_phy(object):

    '''NRC13'''
    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC13_phy]
    @allure.severity('normal')
    @allure.title('物理寻址-默认会话-NRC13测试')
    @allure.description('验证物理寻址默认会话下应响应NRC13')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC13_phy, ids=ids)
    def test_Service_0x19_NRC13_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'noClearFault')

    @allure.severity('normal')
    @allure.title('物理寻址-扩展会话-NRC13测试')
    @allure.description('验证物理寻址扩展会话下应响应NRC13')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC13_phy, ids=ids)
    def test_Service_0x19_NRC13_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'noClearFault')

    '''NRC12'''
    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC12_phy]
    @allure.severity('normal')
    @allure.title('物理寻址-默认会话-NRC12测试')
    @allure.description('验证物理寻址默认会话下应响应NRC12')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_phy, ids=ids)
    def test_Service_0x19_NRC12_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'noClearFault')

    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC12_phy]
    @allure.severity('normal')
    @allure.title('物理寻址-扩展会话-NRC12测试')
    @allure.description('验证物理寻址扩展会话下应响应NRC12')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_phy, ids=ids)
    def test_Service_0x19_NRC12_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'noClearFault')

    '''NRC31'''
    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC31_phy]
    @allure.severity('normal')
    @allure.title('物理寻址-默认会话-NRC31测试')
    @allure.description('验证物理寻址默认会话下应响应NRC31')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC31_phy, ids=ids)
    def test_Service_0x19_NRC31_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'noClearFault')

    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC31_phy]
    @allure.severity('normal')
    @allure.title('物理寻址-扩展会话-NRC31测试')
    @allure.description('验证物理寻址扩展会话下应响应NRC31')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC31_phy, ids=ids)
    def test_Service_0x19_NRC31_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'noClearFault')

    '''NRC13_Priority'''
    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC12_Priority]
    @allure.severity('normal')
    @allure.title('物理寻址-默认会话-NRC12_Priority测试')
    @allure.description('验证物理寻址默认会话下应响应NRC12_Priority')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_Priority, ids=ids)
    def test_reportNumberOfDTC_NRC12_priority_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'noClearFault')

    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC12_Priority]
    @allure.severity('normal')
    @allure.title('物理寻址-扩展会话-NRC12_Priority测试')
    @allure.description('验证物理寻址扩展会话下应响应NRC12_Priority')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_Priority, ids=ids)
    def test_reportNumberOfDTC_NRC12_priority_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'noClearFault')


    '''NRC7F'''
    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC11_phy]
    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-NRC7F测试')
    @allure.description('验证物理寻址编程会话下应响应NRC11')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC11_phy, ids=ids)
    def test_reportNumberOfDTC_NRC11_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False, 'UDS', 'noClearFault')

class Test_APP_ReadDTCInfomation_NRC_Fun(object):
    '''NRC13'''
    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC13_phy]
    @allure.severity('normal')
    @allure.title('功能寻址-默认会话-NRC13测试')
    @allure.description('验证功能寻址默认会话下应响应NRC13')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC13_phy, ids=ids)
    def test_Service_0x19_NRC13_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.title('功能寻址-扩展会话-NRC13测试')
    @allure.description('验证功能寻址扩展会话下应响应NRC13')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC13_phy, ids=ids)
    def test_Service_0x19_NRC13_Extended_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')

    '''NRC12'''
    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC12_phy]
    @allure.severity('normal')
    @allure.title('功能寻址-默认会话-NRC12测试')
    @allure.description('验证功能寻址默认会话下应响应NRC12')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_phy, ids=ids)
    def test_Service_0x19_NRC12_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 1, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.title('功能寻址-扩展会话-NRC12测试')
    @allure.description('验证功能寻址扩展会话下应响应NRC12')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_phy, ids=ids)
    def test_Service_0x19_NRC12_Extended_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 3, iStep, False, 'UDSFun', 'noClearFault')


    '''NRC31'''
    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC31_phy]
    @allure.severity('normal')
    @allure.title('功能寻址-默认会话-NRC31测试')
    @allure.description('验证功能寻址默认会话下应响应NRC31')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC31_phy, ids=ids)
    def test_Service_0x19_NRC31_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 1, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.title('功能寻址-扩展会话-NRC31测试')
    @allure.description('验证功能寻址扩展会话下应响应NRC31')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC31_phy, ids=ids)
    def test_Service_0x19_NRC31_Extended_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 3, iStep, False, 'UDSFun', 'noClearFault')


    '''NRC13_Priority'''
    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC12_Priority]
    @allure.severity('normal')
    @allure.title('功能寻址-默认会话-NRC12_Priority测试')
    @allure.description('验证功能寻址默认会话下应响应NRC12_Priority')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_Priority, ids=ids)
    def test_reportNumberOfDTC_NRC12_priority_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 1, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.title('功能寻址-扩展会话-NRC12_Priority测试')
    @allure.description('验证功能寻址扩展会话下应响应NRC12_Priority')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC12_Priority, ids=ids)
    def test_reportNumberOfDTC_NRC12_priority_Extended_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 3, iStep, False, 'UDSFun', 'noClearFault')


    '''NRC7F'''
    ids = ["Request {0}".format(req, resp) for req, resp in ReadDTCInfo_NRC11_phy]
    @allure.severity('normal')
    @allure.title('功能寻址-编程会话-NRC11测试')
    @allure.description('验证功能寻址编程会话下应响应NRC11')
    @pytest.mark.parametrize('req,resp', ReadDTCInfo_NRC11_phy, ids=ids)
    def test_reportNumberOfDTC_NRC11_Programming_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 2, iStep, False, 'UDSFun', 'noClearFault')